package gc3d.servlet;

import java.io.IOException;
import java.net.MalformedURLException;

import junit.framework.TestCase;

import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class TestWebsiteInlog extends TestCase {

	public void testAccess() throws MalformedURLException, IOException, SAXException {
		// Set-up a conversation with the servlet
		WebConversation wc = new WebConversation();

	
		// Obtain the web page
		WebRequest request = new GetMethodWebRequest(
				"http://pc117b.liacs.nl:8080/example_kienhuis");

		// Send Request to web site
		WebResponse response = wc.getResponse(request);
		System.out.println("Response: " + response.getText());

		assertTrue(response.getText().contains(
				"This is the experimental set up of the a Servlet"));

	}

	public void testServlet() throws MalformedURLException, IOException,
			SAXException {
		// Set-up a conversation with the servlet
		WebConversation wc = new WebConversation();

		// Obtain the web page
		WebRequest request = new GetMethodWebRequest(
				"http://pc117b.liacs.nl:8080/example_kienhuis");

		// Send Request to web site
		WebResponse response = wc.getResponse(request);
		System.out.println("Response: " + response.getText());
		WebLink[] links = response.getLinks();
		for (int i = 0; i < links.length; i++) {
			System.out.println(" LINK:[" + i + " ] = " + links[i].getText());
		}
		WebResponse jdoc = links[0].click();
		System.out.println("Response: " + jdoc.getText());
		assertTrue(jdoc.getText()
				.contains("Example Servlet (doGet) is working"));
	}

	public void testDatabase() throws MalformedURLException, IOException,
			SAXException {
		// Set-up a conversation with the servlet
		WebConversation wc = new WebConversation();

		// Obtain the web page
		WebRequest request = new GetMethodWebRequest(
				"http://pc117b.liacs.nl:8080/example_kienhuis");

		// Send Request to web site
		WebResponse response = wc.getResponse(request);
		System.out.println("Response: " + response.getText());
		WebLink[] links = response.getLinks();
		for (int i = 0; i < links.length; i++) {
			System.out.println(" LINK:[" + i + " ] = " + links[i].getText());
		}
		WebResponse jdoc = links[1].click();
		System.out.println("Response: " + jdoc.getText());
		assertTrue(jdoc.getText().contains("Database opened"));
	}

	public void testLogin() throws MalformedURLException, IOException,
			SAXException {
		// Set-up a conversation with the servlet
		WebConversation wc = new WebConversation();

		// Obtain the web page
		WebRequest request = new GetMethodWebRequest(
				"http://pc117b.liacs.nl:8080/example_kienhuis");

		WebResponse response = wc.getResponse(request);
		System.out.println("Response: " + response.getText());
		WebForm form = response.getForms()[0];
		form.setParameter("name", "kienhuis");
		form.setParameter("passwd", "XXXX");
		form.submit();

		WebResponse jdoc = wc.getCurrentPage();
		System.out.println("Response: " + jdoc.getText());
		assertTrue(jdoc.getText().contains("You typed as login name:kienhuis and as password: XXXX"));
	}
}
